package com.ruoyi.garbage.miniapp;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.garbage.domain.KeywordSearchNum;
import com.ruoyi.garbage.domain.QuestionBank;
import com.ruoyi.garbage.mapper.KeywordSearchNumMapper;
import com.ruoyi.garbage.mapper.QuestionBankMapper;
import com.ruoyi.garbage.service.IKeywordResultService;
import com.ruoyi.garbage.service.IQuestionBankService;
import jakarta.annotation.Resource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 小程序端题库controller
 */
@RestController
@RequestMapping("/miniApp/qb/")
public class QuestionBankMiniAppController {

    @Resource
    IQuestionBankService qbService;
    @Resource
    QuestionBankMapper questionBankMapper;
    @Resource
    IKeywordResultService krService;
    @Resource
    KeywordSearchNumMapper keywordSearchNumMapper;


    @GetMapping("/name/{name}")
    public AjaxResult searchQuestionByName(@PathVariable(value = "name") String name) {
        if (!StringUtils.hasLength(name)) {
            return null;
        }
        QuestionBank questionBank = new QuestionBank();
        questionBank.setGarbageName(name);
        List<QuestionBank> list = qbService.selectQuestionBankList(questionBank);
        return AjaxResult.success(list);
    }

    @GetMapping("/uniname/{name}")
    public AjaxResult searchQuestionByUniName(@PathVariable("name") String name) {
        Map<String, Object> map = qbService.searchQuestionByUniName(name);
        return AjaxResult.success(map);
    }


    @GetMapping("/type/{type}")
    public AjaxResult searchQuestionByType(@PathVariable("type") Integer type) {
        if (type == null) {
            return null;
        }
        QuestionBank questionBank = new QuestionBank();
        questionBank.setGarbageType(type);
        List<QuestionBank> list = qbService.selectQuestionBankList(questionBank);
        return AjaxResult.success(list);
    }

    @GetMapping("/randOne")
    public AjaxResult qbRandOne(Integer num) {
        if (num == null) {
            num = new Random().nextInt(824);
        }
        return AjaxResult.success(questionBankMapper.qbRandOne(num));
    }

    /**
     * 随机获取25个id并查询10条数据
     *
     * @return
     */
    @GetMapping("/randTen")
    public AjaxResult qbRandTen() {
        Integer[] ids = new Integer[25];
        Random random = new Random();
        for (int i = 0; i < ids.length; i++) {
            ids[i] = random.nextInt(824);
        }
        return AjaxResult.success(questionBankMapper.qbRandTen(ids));
    }

    @GetMapping("/top10")
    public AjaxResult topTen(KeywordSearchNum keywordSearchNum) {
        List<KeywordSearchNum> list = keywordSearchNumMapper.selectKeywordSearchNumListTopTen(keywordSearchNum);
        return AjaxResult.success(list);
    }
}

